﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using Roslyn.Compilers.Common;
using Roslyn.Compilers.CSharp;
using RoslynCompiler.MEF;

namespace RoslynCompiler
{
    public class DocumentRewriter : SyntaxRewriter
    {
        [ImportMany]
        private List<ICodeRewriter> codeRewriters;

        public DocumentRewriter()
        {
            var compositionContainer = new CompositionContainer(new DirectoryCatalog("."));
            compositionContainer.ComposeParts(this);
            RewriterIndexMax = codeRewriters.Max(cr => cr.RewriterCount);
        }

        public int RewriterIndexMax { get; private set; }
        public int RewriterIndex { get; set; }
        public ISemanticModel SemanticModel { get; set; }


        public override SyntaxNode Visit(SyntaxNode node)
        {
            if (node == null)
                return null;
            foreach (var codeRewriter in codeRewriters)
                node = codeRewriter.Rewrite(node, RewriterIndex, SemanticModel);
            return node;
        }
    }
}
